const app = getApp()
const $api = require('../../utils/api.js').API;
var common = require('../../utils/common.js');
Page({
  data: {
      form:{
        'education':'高中以下',
        'workTime':"无",
        'birthDay':''},
      avatar:'',
      birthDay:'2000-10-01',
      business:[],
      education:['高中以下','中专','大专','本科','硕士以上'],
      index:0,
      workTime: ['无', '1年', '2年', '3年','4年','5年以上'],
      index1:0,
      studyList:[
        {'name':'培训学校'},
        {'name':'实体店面'},
        {'name':'管理公司'},
        {'name':'美业厂家'},
        {'name':'无'},
      ],
      submittrue:true,
      userTrue:false,
      ruleShow:false,
      img1:'/static/img/pub.png',
      uploadtrue:false,
      imgUrl:app.globalData.imgUrl,
  },
    
  onShow: function(){
    this.setData({
      avatar:wx.getStorageSync('avatarUrl')
    })
    if(wx.getStorageSync('session')){
      var session = wx.getStorageSync('session')
      this.setData({
        form:session
      })
    }else{
      wx.setStorageSync('session',this.data.form);
    }
    this.getTrain();
    this.cateData(this.data.studyList,'studyList')
    this.getIndex(this.data.education,'education','index')
    this.getIndex(this.data.workTime,'workTime','index1')
  },

  getTrain:function(){
    var data = {}
    var that = this
    $api.get('job/getTrain',data).then(res =>{
      if(res.data.code == 1){
        that.cateData(res.data.data,'business');
      }
    })
  },

  bindAllInput: function (e) {
    var inputName = e.currentTarget.dataset.name;
    var form = this.data.form
    form[inputName] = e.detail.value
    wx.setStorageSync('session',form);
    this.setData({
      form:form
    })
  },

  bindBox: function (e) {
    var inputName = e.currentTarget.dataset.name;
    var value = e.detail.value

    let session = wx.getStorageSync('session');
    session[inputName] = value.join(',');
    wx.setStorageSync('session',session);
    this.setData({
      form:session
    })
  },
  bindChange: function(e){
    var inputName = e.currentTarget.dataset.name;
    var index = e.currentTarget.dataset.key
    var value = e.detail.value
    var form = this.data.form
    form[inputName] = this.data[inputName][value]
    wx.setStorageSync('session',form);
    this.setData({
      form:form,
      [index]:value
    })
  },

  getIndex:function(list,key,index){
    var kk = 0;
    if(this.data.form[key]){
      for(var x in list){
        if(list[x] == this.data.form[key]){
          var kk = x;
        }
      }
    }
    this.setData({
      [index]:kk
    })
  },
    addInfo:function(){
      if(!this.data.userTrue){
        wx.showToast({
          title: '请同意用户协议',//提示文字
          duration:3000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading"  
       })
       return;
      }
      this.data.form.uid = wx.getStorageSync('uid')
      var array = {
          'realname':'姓名必填',
          'sex':'性别必选',
          'workType':'工作状态必填',
          'post':'期望工作岗位必填',
          'money':'期望工作薪资',
          'business':'培训专业必选',
          'city':'期望工作城市必选',
          'studyList':'培训经历必选',
          'birthDay':'选择出生日期'
      }
      var message = common.message(array,this.data.form);
      if(message){
        wx.showToast({
          title: message,//提示文字
          duration:3000,//显示时长
          mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
          icon:'none', //图标，支持"success"、"loading"  
       })
        return;
      }
      this.setData({
        submittrue:false
      })
      //调接口 提交
      var that = this
      $api.post('user/add',this.data.form).then(res => {
          //请求成功
          if(res.data.code == 1){
            wx.setStorageSync('role',2)
            //注册完成  清楚缓存
            wx.removeStorageSync('session')
            //拒绝
            if(res.data.data.status == 2){
              wx.showToast({
                title: '申请已拒绝,如有疑问请联系客服!!',//提示文字
                duration:3000,//显示时长
                mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon:'none', //图标，支持"success"、"loading"   
                success:function(){
                  setTimeout(function(){
                    wx.switchTab({
                      url: '../index/index'
                    })
                  },3000) //延迟时间 这里是
                }
              })
            }else{
              //注册成功
              wx.showToast({
                title: '注册已通过!!',//提示文字
                duration:3000,//显示时长
                mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon:'none', //图标，支持"success"、"loading"   
                success:function(){
                  setTimeout(function(){
                    wx.switchTab({
                      url: '../index/index'
                    })
                  },3000) //延迟时间 这里是
                }
              })
            }
          }else{
            //注册失败
            wx.showToast({
              title:res.data.msg,//提示文字
              duration:3000,//显示时长
              mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon:'none', //图标，支持"success"、"loading"   
              success:function(){
                setTimeout(function(){
                  wx.switchTab({
                    url: '../index/index'
                  })
                },3000) //延迟时间 这里是
              }
            })
          }
      })
    },
    bindAcceptRule:function(e){
        if (e.detail.value.length !== 0) {
          this.setData({
            userTrue:true
          })
        }else{
          this.setData({
            userTrue:false
          })
        }
    },
    ruleOk:function(){
      this.setData({
        ruleShow:true
      })
    },
    ruleBack:function(){
      this.setData({
        ruleShow:false
      })
    },
    goCenter:function(e){
      var url = e.currentTarget.dataset.key
      wx.navigateTo({
        url: url
      })
    },
    onShareAppMessage: function(){
      return {
          title: "天意美业直通车",
          path: "/pages/index/index?puid="+wx.getStorageSync('uid')
      };
    },
    cateData:function(cate,key){
      var that = this
      var session = wx.getStorageSync('session');
      if(session[key]){
        let post = session[key].split(',')
        for(var x in cate){
          if(post.indexOf(cate[x]['name']) >= 0){
            cate[x]['checked'] = true
          }
        }
      }
      that.setData({
        [key]:cate
      })
    },
    imgupload:function(e){
      this.setData({
        uploadtrue:true,
        type:e.target.dataset.type
    })
    },
    submit:function(e){
      var key = e.target.dataset.key;
      if(key == 1){
        var sourceType = 'camera'
      }
      if(key == 2){
        var sourceType = 'album'
      }
      var that = this
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: [sourceType], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          wx.uploadFile({
            url: that.data.imgUrl+'/api/img/upload',      //此处换上你的接口地址
            filePath: tempFilePaths[0],
            name: 'img',
            header: {  
              "Content-Type": "multipart/form-data",
              'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
              'token':'tymy'
            },
            formData:{},
            success: function(res){
              var form = that.data.form;
              form.realPic = res.data
              that.setData({
                img1:that.data.imgUrl+res.data,
                form:form,
                uploadtrue:false
              })
            },
            fail: function(res){
              console.log('fail');
            },
          })
        }
      })
    },
    submit3:function(){
      this.setData({
        uploadtrue:false
      })
    },
});